package com.monitor.system.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.monitor.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.monitor.system.mapper.HardDeviceMapper;
import com.monitor.system.domain.HardDevice;
import com.monitor.system.domain.vo.HardDeviceVo;
import com.monitor.system.service.IHardDeviceService;

/**
 * 硬件设备Service业务层处理
 * 
 * @author monitor
 * @date 2024-06-13
 */
@Service
public class HardDeviceServiceImpl implements IHardDeviceService 
{
    @Autowired
    private HardDeviceMapper hardDeviceMapper;

    /**
     * 查询硬件设备
     * 
     * @param id 硬件设备主键
     * @return 硬件设备
     */
    @Override
    public HardDevice selectHardDeviceById(Long id)
    {
        return hardDeviceMapper.selectHardDeviceById(id);
    }

    /**
     * 查询硬件设备列表
     * 
     * @param hardDevice 硬件设备
     * @return 硬件设备
     */
    @Override
    public List<HardDevice> selectHardDeviceList(HardDevice hardDevice)
    {
        return hardDeviceMapper.selectHardDeviceList(hardDevice);
    }

    /**
     * 新增硬件设备
     * 
     * @param hardDevice 硬件设备
     * @return 结果
     */
    @Override
    public int insertHardDevice(HardDevice hardDevice)
    {
        hardDevice.setCreateTime(DateUtils.getNowDate());
        return hardDeviceMapper.insertHardDevice(hardDevice);
    }

    /**
     * 修改硬件设备
     * 
     * @param hardDevice 硬件设备
     * @return 结果
     */
    @Override
    public int updateHardDevice(HardDevice hardDevice)
    {
        hardDevice.setUpdateTime(DateUtils.getNowDate());
        return hardDeviceMapper.updateHardDevice(hardDevice);
    }

    /**
     * 批量删除硬件设备
     * 
     * @param ids 需要删除的硬件设备主键
     * @return 结果
     */
    @Override
    public int deleteHardDeviceByIds(Long[] ids)
    {
        return hardDeviceMapper.deleteHardDeviceByIds(ids);
    }

    /**
     * 删除硬件设备信息
     * 
     * @param id 硬件设备主键
     * @return 结果
     */
    @Override
    public int deleteHardDeviceById(Long id)
    {
        return hardDeviceMapper.deleteHardDeviceById(id);
    }

    /**
     * 查询硬件设备列表
     *
     * @param hardDevice 硬件设备
     * @return 硬件设备
     */
    @Override
    public HardDeviceVo selectInstrumentPanelList(HardDevice hardDevice)
    {
        List<HardDevice> hardDeviceList;
        HardDeviceVo obj = new HardDeviceVo();
        int total = hardDeviceMapper.selectDeviceCount();
        obj.setTotal(total);

        if(hardDevice != null ) {
            hardDeviceList = hardDeviceMapper.selectHardDeviceList(hardDevice);
            // 可用容量
            String availableCapacity = hardDeviceList.get(0).getAvailableCapacity();
            // 总容量
            String totalStorage = hardDeviceList.get(0).getTotalStorage();
            // 使用容量
//            Integer usedCapacity = Integer.valueOf(totalStorage) - Integer.valueOf(availableCapacity);

            if (total > 0) {
                BigDecimal newNumber = new BigDecimal(Integer.valueOf(availableCapacity) * 100).divide(new BigDecimal(totalStorage),1,BigDecimal.ROUND_HALF_UP);
                obj.setPercent(newNumber);
            } else {
                obj.setPercent(new BigDecimal(0));
            }
            obj.setAvailableCapacity(availableCapacity);
            obj.setTotalStorage(totalStorage);
        } else {
            obj.setAvailableCapacity("0");
            obj.setTotalStorage("0");
            obj.setPercent(new BigDecimal(0));
        }
        return obj;
    }
}
